
//
//  MoviesViewController.m
//  WD-Movies
//
//  Created by 贺泌钦 on 2019/1/23.
//  Copyright © 2019 chuck. All rights reserved.
//

#import "MoviesViewController.h"
#import "CollectionViewCell.h"
#import "DCCycleScrollView.h"
#import "MoviesModel.h"
#import "MoviesDetailViewController.h"

@interface MoviesViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout,DCCycleScrollViewDelegate>
@property(nonatomic , strong)UIScrollView *scr;
@property(nonatomic,strong)UICollectionView *clv;
@property(nonatomic , strong)NSMutableArray *datasource;
@property (nonatomic,strong) UILabel *label;


@end

@implementation MoviesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.datasource = [NSMutableArray new];
    self.navigationController.navigationBar.hidden = YES;
    [self setUI];
    [self loadData];
    
//    添加网格
    [self.view addSubview:self.clv];
}
-(void)setUI{
    
    NSArray *imageArr = @[@"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          @"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          @"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          @"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                          ];
    DCCycleScrollView *banner = [DCCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 30, self.view.frame.size.width, 250) shouldInfiniteLoop:YES imageGroups:imageArr];
    //    banner.placeholderImage = [UIImage imageNamed:@"placeholderImage"];
    //    banner.cellPlaceholderImage = [UIImage imageNamed:@"placeholderImage"];
    banner.autoScrollTimeInterval = 2;
    banner.autoScroll = YES;
    banner.isZoom = YES;
    banner.itemSpace = 0;
    banner.imgCornerRadius = 10;
    banner.itemWidth = self.view.frame.size.width - 200;
    banner.delegate = self;
    
     [self.view addSubview:banner];
}
//点击图片的代理
-(void)cycleScrollView:(DCCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"index = %ld",(long)index);
}
//网格
- (UICollectionView *)clv{
    if (!_clv) {
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(126, 164);
        layout.headerReferenceSize = CGSizeMake(WIDTH,50);
        _clv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 300, WIDTH, HEIGHT-250) collectionViewLayout:layout];
        _clv.backgroundColor =[UIColor whiteColor];
        _clv.delegate = self;
        _clv.dataSource = self;
        [_clv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        //注册头视图
        [_clv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
        
    }
    return _clv;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _datasource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    MoviesModel *model = self.datasource[indexPath.row];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 126, 164)];
    img.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.imageUrl]]]];
        [cell addSubview:img];
    
    
    return cell;
}

//  返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        //添加头视图的内容
        [self addContent];
        //头视图添加view
        [header addSubview:self.label];
        return header;
    }
    return nil;
}
//头部的label
-(void)addContent
{
    NSArray *arr= @[@"热门电影",@"正在上映",@"即将上映"];
    for (int i = 0; i<3; i++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 40)];
        label.text = arr[i];
        label.textColor = [UIColor blackColor];
        self.label = label;
    }
}
//数据请求部分
-(void)loadData{
//    NSDictionary *pdic = @{@"userId":@"18",@"sessionId":@"15320748258726",@"page":@"1",@"count":@"10"};
    
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
//    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manger GET:@"http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userId=18&sessionId=15320748258726&page=1&count=10" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@",responseObject);
        for (NSDictionary *dic in responseObject[@"result"]) {
            MoviesModel *model =[MoviesModel new];
            [dic objectForKey:@"imageUrl"];
           
            [model setValuesForKeysWithDictionary:dic];
            [self.datasource addObject:model];
        }
//        self.model = [MoviesModel yy_modelWithJSON:responseObject];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.clv reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MoviesDetailViewController *detail = [[MoviesDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}



@end
